package com.yiyan.study.controller;

import com.yiyan.study.utils.redis.RedisLockService;
import com.yiyan.study.utils.redis.annotation.RedissonLock;
import jakarta.annotation.Resource;
import jodd.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author Alex Meng
 * @createDate 2023-12-30 00:30
 */
@RestController
@RequestMapping("/")
@Slf4j
public class RedisLockController {

    @Resource
    private RedisLockService redisLockService;

    @GetMapping("/lock_func")
    @RedissonLock(prefixKey = "lockFunc", waitTime = 500)
    public void lockFunc() {
        // 模拟查询等不需要锁的操作
        ThreadUtil.sleep(2000L);
        // 模拟上锁的数据操作
        ThreadUtil.sleep(200L);
    }


    @GetMapping("/lock_line")
    public void lockLine() {
        // 模拟查询等不需要锁的操作
        ThreadUtil.sleep(2000L);
        redisLockService.executeWithLock("lockLine", 500, TimeUnit.MILLISECONDS,
                () -> {
                    // 模拟上锁的数据操作
                    ThreadUtil.sleep(200L);
                    return null;
                });
    }
}
